/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 * Description: 与AI设备(D芯片)相关hdds接口适配
 * Author: linzhiyang
 * Create: 2023-01-17
 */

#include <adapter_hdds.h>
#include "log.h"
#include "dlhdds_function.h"

using namespace hccl;
constexpr s32 PARTITIONMAP_ALREADY_EXISTS = -2;

HcclResult hrtQueryPartitionMapPsId(u64 key, u32 *psId)
{
    CHK_PTR_NULL(psId);
    s32 ret = DlHddsFunction::GetInstance().dlQueryPartitionMapPsId(key, psId);
    CHK_PRT_RET(ret != HCCL_SUCCESS,
        HCCL_ERROR("[Query][PartitionMapPsId]errNo[0x%016llx] hdds query partitionMap psId fail. "
        "return[0x%016llx], params: key[%llu].", HCCL_ERROR_CODE(HCCL_E_INTERNAL), ret, key), HCCL_E_INTERNAL);
    return HCCL_SUCCESS;
}

HcclResult hrtInitPartitionMap(u32 partitionNum, u32 psNum, const u32 psId[])
{
    CHK_PTR_NULL(psId);
    s32 ret = DlHddsFunction::GetInstance().dlInitPartitionMap(partitionNum, psNum, psId);
    if (ret == PARTITIONMAP_ALREADY_EXISTS) {
        HCCL_WARNING("[Init][PartitionMap] hdds init partitionMap exited.");
        return HCCL_SUCCESS;
    }
    CHK_PRT_RET(ret != HCCL_SUCCESS,
        HCCL_ERROR("[Init][PartitionMap]errNo[0x%016llx] hdds init partitionMap fail. "
        "return[0x%016llx], params: partitionNum[%u], psNum[%u].", HCCL_ERROR_CODE(HCCL_E_INTERNAL), ret,
        partitionNum, psNum), HCCL_E_INTERNAL);
    return HCCL_SUCCESS;
}

HcclResult hrtGetBatchPsIds(u64 *keys, u32 *psIds[], u32 num)
{
    CHK_PTR_NULL(keys);
    CHK_PTR_NULL(psIds);
    s32 ret = DlHddsFunction::GetInstance().dlGetBatchPsIds(keys, psIds, num);
    CHK_PRT_RET(ret != HCCL_SUCCESS,
        HCCL_ERROR("[Get][BatchPsIds]errNo[0x%016llx] hdds Get BatchPsIds fail. "
        "return[0x%016llx], params: num[%u].", HCCL_ERROR_CODE(HCCL_E_INTERNAL), ret, num), HCCL_E_INTERNAL);
    return HCCL_SUCCESS;
}